package com.asdan.seededu.service.impl;


import java.util.ArrayList;

import java.util.HashMap;

import com.asdan.seededu.service.AlipayService;
import com.asdan.seededu.util.DatabaseAccess;
import com.asdan.seededu.util.EasyMapsManager;

public class AlipayServiceImpl implements AlipayService {

	public boolean InsertOrder(String ordersid,String uid, String out_trade_no, String subject,
			String price, String body, String show_url, String receive_name,
			String receive_address, String receive_zip, String receive_phone,
			String receive_mobile, String update_time) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO `order`(`uid`,`out_trade_no`,`subject`,`price`,`body`,`show_url`,`receive_name`,`receive_address`,`receive_zip`,`receive_phone`,`receive_mobile`,`update_time`,`orders_id`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			databaseAccess.setPreparedParameter(Integer.parseInt(uid));
			databaseAccess.setPreparedParameter(out_trade_no);
			databaseAccess.setPreparedParameter(subject);
			databaseAccess.setPreparedParameter(price);
			databaseAccess.setPreparedParameter(body);
			databaseAccess.setPreparedParameter(show_url);
			databaseAccess.setPreparedParameter(receive_name);
			databaseAccess.setPreparedParameter(receive_address);
			databaseAccess.setPreparedParameter(receive_zip);
			databaseAccess.setPreparedParameter(receive_phone);
			databaseAccess.setPreparedParameter(receive_mobile);
			databaseAccess.setPreparedParameter(update_time);
			databaseAccess.setPreparedParameter(Integer.parseInt(ordersid));
			databaseAccess.executeInsert(sql);
			databaseAccess.close();
			return true;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean InsertOrderReply(String oid,String uid,String body,
			String buyer_email, String buyer_id, String discount,
			String gmt_create, String gmt_logistics_modify, String gmt_payment,
			String is_success, String is_total_fee_adjust,
			String logistics_fee, String logistics_payment,
			String logistics_type, String price, String quantity,
			String receive_address, String receive_mobile, String receive_name,
			String receive_phone, String receive_zip, String seller_actions,
			String seller_email, String seller_id, String subject,
			String total_fee, String trade_no, String trade_status,
			String use_coupon, String sign, String sign_type, String update_time) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO `order_reply`(`body`,`buyer_email`,`buyer_id`,`discount`,`gmt_create`," +
				"`gmt_logistics_modify`,`gmt_payment`,`is_success`,`is_total_fee_adjust`,`logistics_fee`,`logistics_payment`," +
				"`logistics_type`,`price`,`quantity`,`receive_address`,`receive_mobile`,`receive_name`,`receive_phone`," +
				"`receive_zip`,`seller_actions`,`seller_email`,`seller_id`,`subject`,`total_fee`,`trade_no`,`trade_status`," +
				"`use_coupon`,`sign`,`sign_type`,`update_time`,`uid`,`oid`) " +
				"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			databaseAccess.setPreparedParameter(body);
			databaseAccess.setPreparedParameter(buyer_email);
			databaseAccess.setPreparedParameter(buyer_id);
			databaseAccess.setPreparedParameter(discount);
			databaseAccess.setPreparedParameter(gmt_create);
			databaseAccess.setPreparedParameter(gmt_logistics_modify);
			databaseAccess.setPreparedParameter(gmt_payment);
			databaseAccess.setPreparedParameter(is_success);
			databaseAccess.setPreparedParameter(is_total_fee_adjust);
			databaseAccess.setPreparedParameter(logistics_fee);
			databaseAccess.setPreparedParameter(logistics_payment);
			databaseAccess.setPreparedParameter(logistics_type);
			databaseAccess.setPreparedParameter(price);
			databaseAccess.setPreparedParameter(quantity);
			databaseAccess.setPreparedParameter(receive_address);
			databaseAccess.setPreparedParameter(receive_mobile);
			databaseAccess.setPreparedParameter(receive_name);
			databaseAccess.setPreparedParameter(receive_phone);
			databaseAccess.setPreparedParameter(receive_zip);
			databaseAccess.setPreparedParameter(seller_actions);
			databaseAccess.setPreparedParameter(seller_email);
			databaseAccess.setPreparedParameter(seller_id);
			databaseAccess.setPreparedParameter(subject);
			databaseAccess.setPreparedParameter(total_fee);
			databaseAccess.setPreparedParameter(trade_no);
			databaseAccess.setPreparedParameter(trade_status);
			databaseAccess.setPreparedParameter(use_coupon);
			databaseAccess.setPreparedParameter(sign);
			databaseAccess.setPreparedParameter(sign_type);
			databaseAccess.setPreparedParameter(update_time);
			databaseAccess.setPreparedParameter(Integer.parseInt(uid));
			databaseAccess.setPreparedParameter(Integer.parseInt(oid));
			databaseAccess.executeInsert(sql);
			databaseAccess.close();
			return true;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public String SelectOrderId(String uid, String out_trade_no) {
		// TODO Auto-generated method stub
		String sql = "select o.`orders_id` from `order` o where o.uid=? and o.out_trade_no=?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(Integer.parseInt(uid));
			easyMapsManager.setPreparedParameter(out_trade_no);
			@SuppressWarnings("rawtypes")
			ArrayList<HashMap> list = easyMapsManager.executeQuery(sql);
			@SuppressWarnings("rawtypes")
			HashMap hm = list.get(0);
			databaseAccess.close();
			return hm.get("orders_id").toString();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String SelectOrdersId(String uid, String orders_id) {
		// TODO Auto-generated method stub
		String sql = "select o.`id` from `order` o where o.uid=? and o.orders_id=? order by o.`Id`;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(Integer.parseInt(uid));
			easyMapsManager.setPreparedParameter(Integer.parseInt(orders_id));
			@SuppressWarnings("rawtypes")
			ArrayList<HashMap> list = easyMapsManager.executeQuery(sql);
			HashMap hm =null;
			if(list.size()>0)
			{
				hm = list.get(list.size()-1);
				databaseAccess.close();
				return hm.get("id").toString();
			}
			else
			{
				databaseAccess.close();
				return null;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean updateOrder(String uid, String orders_id,
			String out_trade_no, String update_time) {
		// TODO Auto-generated method stub
		String sql = "UPDATE `order` SET out_trade_no=?,update_time=? WHERE uid=? AND orders_id=?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			databaseAccess.setPreparedParameter(out_trade_no);
			databaseAccess.setPreparedParameter(update_time);
			databaseAccess.setPreparedParameter(Integer.parseInt(uid));
			databaseAccess.setPreparedParameter(Integer.parseInt(orders_id));
			databaseAccess.executeUpdate(sql);
			databaseAccess.close();
			return true;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}

}
